//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus.dataAccess.asyncTasks;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

import com.bc004346.travelus.dataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.model.interfaces.ILoginCallback;
import com.bc004346.travelus.utility.Utility;

/**
 * Created by Renats on 1/27/14.
 * Async fragment for Login action
 */
public class LoginTaskFragment extends Fragment {

    //<editor-fold desc="Members">
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private TravelUsScreenScraper mTravelUs;
    private ILoginCallback mCallbacks;
    //</editor-fold>

    //<editor-fold desc="Static Methods">
    public static LoginTaskFragment newInstance(String username, String password) {
        LoginTaskFragment ltf = new LoginTaskFragment();
        Bundle args = new Bundle();
        args.putString(USERNAME, username);
        args.putString(PASSWORD, password);
        ltf.setArguments(args);
        return ltf;
    }
    //</editor-fold>

    //<editor-fold desc="Overrides">
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (ILoginCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mTravelUs = Utility.getInstance().getTravelUS();
        String username = getArguments() == null ? "" : getArguments().getString(USERNAME);
        String password = getArguments() == null ? "" : getArguments().getString(PASSWORD);
        new LoginTask().execute(username, password);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    //</editor-fold>

    private class LoginTask extends AsyncTask<String, Void, Boolean> {

        Exception ex = null;

        @Override
        protected void onPostExecute(Boolean success) {
            if (mCallbacks != null) {
                mCallbacks.onLoginPostExecute(success, ex);
            }
        }

        @Override
        protected Boolean doInBackground(String... params) {

            Boolean result = false;

            try {
                result = mTravelUs.doLogin(params[0], params[1]);
            } catch (Exception e) {
                ex = e;
            }
            return result;
        }
    }
}
